package com.ktg.mes.erpInventoryCat.service.impl;

import com.ktg.mes.md.domain.ItemType;
import com.ktg.mes.erpInventoryCat.entity.T6ErpInventoryCategory;
import com.ktg.mes.erpInventoryCat.service.IErpInventoryCategoryService;
import com.ktg.mes.erpInventoryCat.service.IT6ErpInventoryCategoryService;
import com.ktg.mes.md.mapper.ItemTypeMapper;
import com.ktg.mes.md.service.IItemTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ErpInventoryCategoryServiceImpl implements IErpInventoryCategoryService {
    private static final Logger log = LoggerFactory.getLogger(ErpInventoryCategoryServiceImpl.class);

    @Autowired
    private IT6ErpInventoryCategoryService it6ErpInventoryCategoryService;

    @Autowired
    private IItemTypeService iItemTypeService;

    @Autowired
    private ItemTypeMapper itemTypeMapper;

    @Override
    public void syncDataListFromErp() {
        List<T6ErpInventoryCategory> srcList = it6ErpInventoryCategoryService.queryList();
        Map<String,ItemType> itemTypeMap = null;
        if (srcList.size() > 0) {
            List<ItemType> srcItemTypeList = itemTypeMapper.listAll();
            itemTypeMap = srcItemTypeList.stream().collect(Collectors.toMap(ItemType::getItemTypeCode,ItemType -> ItemType));
        }
        for (T6ErpInventoryCategory srcItem : srcList) {
            ItemType distItem = new ItemType();
            distItem.setItemTypeCode(srcItem.getId());
            distItem.setItemTypeName(srcItem.getInvCategoryName());
            distItem.setItemOrProduct("PRODUCT");
            if (srcItem.getInvGrade() > 1) {
                String parentInvCode = srcItem.getId().substring(0,srcItem.getId().length() - 2);
                ItemType parentItemType = itemTypeMap.get(parentInvCode); // 父节点的信息
                if (parentItemType != null && parentItemType.getParentTypeId() != null) {
                    distItem.setParentTypeId(parentItemType.getItemTypeId()); // 设置当前节点的父节点ID
                    distItem.setAncestors(parentItemType.getAncestors() + "," + parentItemType.getItemTypeId());
                }else {
                    continue;
                }

                // 修改当前节点的父节点 has_child 属性
               /* MesInventoryCategory parentItem = iMesInventoryCategoryService.getById(pid);
                if (parentItem != null && "0".equals(parentItem.getHasChild())){
                    parentItem.setHasChild("1");
                    iMesInventoryCategoryService.updateById(parentItem);
                }*/

            }else {
                distItem.setParentTypeId(1L);
                distItem.setAncestors(",1");
            }
            distItem.setItemTypeCode(srcItem.getId());
            if (itemTypeMap.containsKey(distItem.getItemTypeCode())){
                iItemTypeService.updateItemType(distItem);
            }else {
                distItem.setEnableFlag("Y");
                iItemTypeService.insertItemType(distItem);
                itemTypeMap.put(distItem.getItemTypeCode(),distItem);
            }

        }
    }

    public static void main(String[] args) {
        String pid = "010101";
        System.out.println(pid.substring(0,pid.length() - 2 ));
    }
}
